<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<html>

<head>
  <meta charset="UTF-8">
  <title>知识库-后台管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet"  th:href="@{/css/font.css}">
  <link rel="stylesheet"  th:href="@{/css/xadmin.css}">
  <script type="text/javascript" th:src="@{/plugins/jquery/jquery.min.js}" ></script>
  <script  th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
  <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body class="layui-anim layui-anim-up">

<div class="x-body">
  <div class="layui-row">
    <form class="layui-form layui-col-md12 x-so">
      <input class="layui-input" placeholder="开始日" name="start" id="start">
      <input class="layui-input" placeholder="截止日" name="end" id="end">
      <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
      <button class="layui-btn layui-btn-normal"  lay-submit="" lay-filter="sreach" onclick="sreach()"><i class="layui-icon">&#xe615;</i></button>
      <a class="layui-btn layui-btn-small layui-btn-normal" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </form>
  </div>
  <table id="demo" lay-filter="test"></table>
</div>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delall"><i class="layui-icon"></i>批量删除</button>
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" onclick="x_admin_show('添加用户','/user/toAdd',600,550)"><i class="layui-icon"></i>添加</button>
  </div>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail"><i class="layui-icon layui-icon-search"></i></a>
  <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
    <i class="layui-icon layui-icon-delete"></i></a>
</script>
<script>
  layui.use('table', function(){
    var table = layui.table
            ,form = layui.form;


    //第一个实例
    var text = table.render({
      elem: '#demo'
      ,height: 312
      ,url: '/user/getUsers' //数据接口
      ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
      ,defaultToolbar: ['filter', 'exports', 'print']
      ,title: '用户数据表'
      ,cols: [
        [ //表头
          {type: 'checkbox', fixed: 'left'}
          ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
          ,{field: 'name', title: '用户名', width:150}
          ,{field: 'sex', title: '性别', width:150}
          ,{field: 'email', title: '邮箱', width:150}
          ,{field: 'role', title: '权限', width:150}
          ,{field: 'createTime', title: '创建时间', width:150}
          ,{field: 'state', width: 90, title: '状态', event: 'setStatus', templet: function (res) {
            let menuId = res.id;
            if (res.state == 1) {
              return "   <input type='checkbox'  menuId = '" + menuId + "' lay-filter='state' lay-skin='switch' lay-text='ON|OFF' checked>"
            } else if (res.state == 0) {
              return "   <input type='checkbox'  menuId = '" + menuId + "'  lay-filter='state' lay-skin='switch' lay-text='ON|OFF'>"
            }
          }
        }
          ,{fixed: 'right', width:200, align:'center', toolbar: '#barDemo'}
        ]
      ]
      ,page: true //开启分页
    });
    table.on('toolbar(test)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'delall':
          layer.confirm("确认删除吗？",function () {
            var data = checkStatus.data;
            for (i = 0; i < data.length; i++) {
              deleteUser(data[i].id);
              obj.del();
            }
          })
          break;
      };
    });
    //监听事件
    table.on('tool(test)', function(obj){
      //var checkStatus = table.checkStatus(obj.config.id);
      var data = obj.data;
      switch(obj.event){
        case 'detail':
          x_admin_show('查看信息','/user/toDetail/'+data.id,600,500);
          break;
        case 'del':
          layer.confirm("确认删除吗？",function () {
            deleteUser(data.id);
            obj.del();
          })
          break;
        case 'edit':
          x_admin_show('编辑用户','/user/toEdit/'+data.id,600,550);
          break;
      };
    });
    form.on('switch(state)', function (obj) {
      //当前元素
      var data = $(obj.elem);
      //遍历父级tr，取第一个，然后查找第二个td，取值
      var id = data.parents('tr').first().find('td').eq(1).text();
      state(id)
    });
    function state(id) {
      $.ajax({
        url:"/user/state/"+id,
        dataType: "json",
        success:function (data) {
          layer.msg(data.msg);
        }
      });
    }
    function deleteUser(id) {
      $.ajax({
        url:"/user/delete/"+id,
        dataType:"json",
        success:function (data) {
          layer.msg(data.msg);
        }
      });
    }
    function sreach(){
      layui.use('layer', function(){
        var layer = layui.layer;
        layer.open({
          text
        });
      });
    }
  });
</script>
</body>

</html>